/*
 * @Author       : 邱狮杰
 * @Date         : 2021-05-19 14:34:32
 * @LastEditTime : 2021-05-19 14:51:13
 * @FilePath     : /wedding-dress-selection/src/utils/Synchronization.ts
 * @Description  :
 */
export function SynchronizationAwaitError<T, B = T, E = Error>(promise: Promise<T>, beautifyReturnValue?: (res: T) => B, errorCaptured?: object): Promise<[E, null] | [null, T | B]> {
  return promise
    .then<[null, T | B]>((result: T) => {
      return [null, beautifyReturnValue ? beautifyReturnValue(result) : result]
    })
    .catch<[E, null]>((causeOfError: E) => {
      if (errorCaptured) return [Object.assign(causeOfError, errorCaptured), null]
      return [causeOfError, null]
    })
}
